import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import 'font-awesome/css/font-awesome.min.css'
import axios from 'axios'
import qs from 'qs'
import BigNumber from 'bignumber.js'
import {
  getCurDate,
  setSessionStorage,
  getSessionStorage,
  removeSessionStorage,
  setLocalStorage,
  getLocalStorage,
  removeLocalStorage
} from './common.js'

//创建应用实例
const app = createApp(App);


app.config.productionTip = false

//设置axios的基础url部分
axios.defaults.baseURL = 'http://localhost:8080/elm/';
//axios.defaults.baseURL = 'http://47.104.90.43:8080/elm/';

//将axios和其他全局方法挂载到应用实例上，使用时就可以 this.$axios 这样使用了
app.config.globalProperties.$axios = axios;
app.config.globalProperties.$qs = qs;
app.config.globalProperties.$BigNumber = BigNumber;

app.config.globalProperties.$getCurDate = getCurDate;
app.config.globalProperties.$setSessionStorage = setSessionStorage;
app.config.globalProperties.$getSessionStorage = getSessionStorage;
app.config.globalProperties.$removeSessionStorage = removeSessionStorage;
app.config.globalProperties.$setLocalStorage = setLocalStorage;
app.config.globalProperties.$getLocalStorage = getLocalStorage;
app.config.globalProperties.$removeLocalStorage = removeLocalStorage;



// 路由守卫
router.beforeEach((to, from, next) => {
  const user = sessionStorage.getItem('user');
  // 除了登录、注册、首页、商家列表、商家信息之外，都需要判断是否登录
  if (!['/', '/index', '/businessList', '/businessInfo', '/login', '/register', '/find'].includes(to.path)) {
    if (user == null) {
      router.push({ path: '/login' });
      //location.reload();
      next();
    }
  }
  next();
});

app.use(router).mount('#app');
